//
// Created by liuping on 2018/12/24.
//

#include <iostream>
#include "co/co_task.h"
#include "worker.h"
#include "worker_group.h"

using namespace std;
struct T {
    T(int a) : a_(a) {}
    void f(const CoYield &co) {
        cout << a_<< " 1\n";
        co.Yield();
        cout << a_<<" 2\n";
        co.Yield();
        cout << a_<< " 3\n";
    }
    int a_ = 0;
};

void f(const CoYield &co) {
    cout << " 1\n";
    co.Yield();
    cout << " 2\n";
    co.Yield();
    cout << " 3\n";
}

#include <thread>
#include <chrono>

int main() {

    // add tack
    try {
        WorkerGroup<int> worker_group(2);
        worker_group.Co(0).AddTack(std::bind(&T::f, std::make_shared<T>(1), std::placeholders::_1));
        worker_group.Co(0).AddTack(std::bind(&T::f, std::make_shared<T>(2), std::placeholders::_1));
        worker_group.Co(1).AddTack(std::bind(f, std::placeholders::_1));
        worker_group.Co(2).AddTack(std::bind(f, std::placeholders::_1));

        for (int i = 0; i < 4; i++) {
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
            worker_group.Msg(0).AddMsg(1);
            worker_group.Msg(0).AddMsg(2);
            worker_group.Msg(1).AddMsg(1);
            worker_group.Msg(1).AddMsg(2);
        }
        worker_group.StopAll();
    } catch (std::logic_error e) {
        std::cout << e.what() << std::endl;
    }
    return 0;
}
